package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketPermission;
import com.cskaoyan.market.db.domain.MarketPermissionExample;
import com.cskaoyan.market.db.domain.MarketRole;
import com.cskaoyan.market.db.domain.MarketRoleExample;
import com.cskaoyan.market.db.mapper.MarketPermissionMapper;
import com.cskaoyan.market.db.mapper.MarketRoleMapper;
import com.cskaoyan.market.service.AdminRoleService;
import com.cskaoyan.market.util.ResponseUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

/**
 * @author 刘国兴
 * @description 管理员角色\
 *
 */
@Service
public class AdminRoleServiceImpl implements AdminRoleService {

    @Autowired
    MarketRoleMapper mapper;
    @Autowired
    MarketPermissionMapper marketPermissionMapper;


    @Override
    public Object options() {
        List<MarketRole> marketRoles = mapper.selectByExampleSelective(new MarketRoleExample(), MarketRole.Column.id, MarketRole.Column.name);
        Map<String, Object> roleMap = null;
        ArrayList<Map> roleList = new ArrayList<>();
        for (MarketRole marketRole : marketRoles) {
            roleMap = new HashMap<>();
            roleMap.put("value", marketRole.getId());
            roleMap.put("label", marketRole.getName());
            roleList.add(roleMap);
        }
        return  ResponseUtil.okList(roleList);
    }

    @Override
    public Set<String> selectPermission(Integer roleId) {
        Set<String> assignedPermissions = new HashSet<>();
        MarketPermissionExample marketPermissionExample = new MarketPermissionExample();
        marketPermissionExample.createCriteria().andRoleIdEqualTo(roleId);
        List<MarketPermission> marketPermissions = marketPermissionMapper.selectByExample(marketPermissionExample);
        for (MarketPermission marketPermission : marketPermissions) {
            assignedPermissions.add(marketPermission.getPermission());
        }
        return assignedPermissions;
    }

    @Override
    public List<MarketRole> seletAllroles(Integer page, Integer limit, String sort, String order) {

        MarketRoleExample marketRoleExample = new MarketRoleExample();
        marketRoleExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page,limit);
        return mapper.selectByExample(marketRoleExample);
    }

    @Override
    public void insertPermissions(List<String> permissions, Integer roleId) {

        MarketPermissionExample marketPermissionExample = new MarketPermissionExample();
        marketPermissionExample.createCriteria().andRoleIdEqualTo(roleId);
        marketPermissionMapper.deleteByExample(marketPermissionExample);
        if(permissions == null) return;
        for (String permission : permissions) {
            MarketPermission marketPermission = new MarketPermission();
            marketPermission.setDeleted(false);
            marketPermission.setAddTime(LocalDateTime.now());
            marketPermission.setPermission(permission);
            marketPermission.setRoleId(roleId);
            marketPermission.setUpdateTime(LocalDateTime.now());
            marketPermissionMapper.insert(marketPermission);
        }


    }
}
